﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using KeaoBase;

namespace KeaoMDS.InfoManage
{
    public partial class DanWeiGuanLi : BaseForm
    {
        private bool m_IsRuKu = false;

        public DanWeiGuanLi(bool IsRuKu)
        {
            InitializeComponent();
            m_IsRuKu = IsRuKu;
            if (IsRuKu)
            {
                this.Text = "入库单位管理";
                col_gongyingshang.Visible = true;
            }
            else
            {
                this.Text = "出库单位管理";
                col_gongyingshang.Visible = false;
            }
        }

        private void DanWeiGuanLi_Load(object sender, EventArgs e)
        {
            dataGridView1.AutoGenerateColumns = false;
            BindInfo();
        }

        private void menu_1_xinzeng_Click(object sender, EventArgs e)
        {
            if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("单位管理"))
            {
                KeaoUtils.ShowInfoMessage("该用户没有操作权限。");
                return;
            }

            DanWeiBianJi danWeiBianJi = new DanWeiBianJi(m_IsRuKu);
            if (danWeiBianJi.ShowDialog() == DialogResult.OK)
            {
                BindInfo();
                dataGridView1.Rows[dataGridView1.Rows.Count - 1].Selected = true;
            }
        }

        private void menu_1_xiugai_Click(object sender, EventArgs e)
        {
            if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("单位管理"))
            {
                KeaoUtils.ShowInfoMessage("该用户没有操作权限。");
                return;
            }
            
            if (dataGridView1.SelectedRows.Count==0)
            {
                return;
            }
            int selectIndex = dataGridView1.SelectedRows[0].Index;
            DanWeiBianJi danWeiBianJi = new DanWeiBianJi(dataGridView1.SelectedRows[0],m_IsRuKu);
            if (danWeiBianJi.ShowDialog() == DialogResult.OK)
            {
                BindInfo();
                dataGridView1.Rows[selectIndex].Selected = true;
            }
        }

        private void menu_1_shanchu_Click(object sender, EventArgs e)
        {
            if (!string.Format("{0}", KeaoUtils._CurrentUserRow["juesequanxian"]).Contains("单位管理"))
            {
                KeaoUtils.ShowInfoMessage("该用户没有操作权限。");
                return;
            }

            if (dataGridView1.SelectedRows.Count==0)
            {
                return;
            }
            if (KeaoUtils.ShowQuestionMessage("是否要删除该单位信息？") == DialogResult.Yes)
            {
                if (KeaoUtils._DB.DeleteRowByID(new TableRow() { TableName = "tbl_danwei", ID = string.Format("{0}", dataGridView1.SelectedRows[0].Cells["col_id"].Value) }) > 0)
                {
                    KeaoUtils.ShowInfoMessage("删除单位信息完成。");
                    BindInfo();
                }
            }
        }

        private void BindInfo()
        {
            string createCondition = string.Empty;
            if (m_IsRuKu)
            {
                createCondition = KeaoUtils._DB.CreateAndCondition("danweileixing", DataValueCommonType.字符串, "入库单位", "", SearchConditionType.等于);
            }
            else
            {
                createCondition = KeaoUtils._DB.CreateAndCondition("danweileixing", DataValueCommonType.字符串, "出库单位", "", SearchConditionType.等于);
            }
            DataTable Dt = KeaoUtils._DB.GetDataTableByCondition("tbl_danwei", createCondition);
            dataGridView1.DataSource = Dt;
        }

        private void dataGridView1_RowStateChanged(object sender, DataGridViewRowStateChangedEventArgs e)
        {
            e.Row.HeaderCell.Value = string.Format("{0}",e.Row.Index + 1);
        }

    }
}
